package io.yuanqi.hgtcshop.controller;


import com.alibaba.fastjson.JSON;
import io.yuanqi.hgtcshop.entity.dos.Order;
import io.yuanqi.hgtcshop.entity.enums.ResultCode;
import io.yuanqi.hgtcshop.entity.param.OrderParam;
import io.yuanqi.hgtcshop.entity.vo.OrderVO;
import io.yuanqi.hgtcshop.entity.vo.ResultMessage;
import io.yuanqi.hgtcshop.entity.vo.UserNameAndFaceVO;
import io.yuanqi.hgtcshop.exception.ServiceException;
import io.yuanqi.hgtcshop.service.OrderService;
import io.yuanqi.hgtcshop.utils.ResultUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 买家端，订单接口
 *
 * @author Chopper
 * @since 2020/11/16 10:04 下午
 */
@Slf4j
@RestController
//@Api(tags = "买家端，订单接口")
@RequestMapping("/buyer/trade/order")
public class OrderController {

    /**
     * 购物车
     */
    @Autowired
    private OrderService orderService;




//   //  @ApiOperation(value = "创建订单")
    @PostMapping(value = "/create")
    public ResultMessage<String> crateTrade(OrderParam orderParam) {
        try {
            //读取选中的列表
            log.info(".create 创建订单 入参为：{}",JSON.toJSONString(orderParam));
            String orderTrade = this.orderService.createOrderTrade(orderParam.getOpenId(), orderParam.getSkuIds());
            log.info(".create 返回的结果为：{}",orderTrade);
            return ResultUtil.data(orderTrade);
        } catch (ServiceException se) {
            log.info(se.getMsg(), se);
            throw se;
        } catch (Exception e) {
            log.error(ResultCode.ORDER_ERROR.message(), e);
            throw e;
        }
    }

    // 查看订单
    @GetMapping(value = "/getOrderBySn")
    public ResultMessage<OrderVO> getOrderBySn(String orderSn) {
        log.info(".getOrderBySn 接受到请求 orderSn:{}",orderSn);
        OrderVO orderBySn = this.orderService.getOrderBySn(orderSn);
        log.info(".getOrderBySn 返回结果 result:{}", JSON.toJSONString(orderBySn));
        return ResultUtil.data(orderBySn);
    }
    // 编辑订单状态

    // 删除订单


    /**
     * 我的订单
     * @param memberId 当前的会员id
     * @param orderType  订单类型 当前订单 = 0 = 待付款 /历史订单 = 1 = 已付款 and 取消
     * */
    @GetMapping(value = "/getOrderByMemberId")
    public ResultMessage<List<OrderVO>> getOrderByMemberId(String memberId, Integer orderType){
        return ResultUtil.data(this.orderService.getOrderByMemberId(memberId,orderType));
    }


    @GetMapping(value = "/getUserNameAndFaceByOrderSn")
    public ResultMessage<UserNameAndFaceVO> getUserNameAndFaceByOrderSn(String orderSn){
        return ResultUtil.data(this.orderService.getUserNameAndFaceByOrderSn(orderSn));
    }

    @GetMapping(value = "/getOrderInfo")
    public ResultMessage<Order> getOrderInfo(String orderSn){
        log.info(".getOrderInfo orderSn:{}",orderSn);
        Order orderInfo = this.orderService.getOrderInfo(orderSn);
        log.info(".getOrderInfo  result:{}", JSON.toJSONString(orderInfo));
        return ResultUtil.data(orderInfo);
    }


    @PostMapping(value = "/updateAddress")
    public ResultMessage<Boolean> updateAddress(String orderSn,String openId) {
        Boolean isTrue = orderService.updateAddress(orderSn,openId);
        return ResultUtil.data(isTrue);
    }
}
